mtex: texture map maker

TOP | Up | Download | Pheedbak | Tree | Topic | A-Z | Search | Hot | New


mtex

By Doug Voorhies
"mtex" is a small image processing application oriented
to massaging scanned images for use as texture maps.

"mtex" performs common operations such as color or contrast correction, cropping, and sharpening, plus unusual functions needed for texture maps. These include making edges match so the image can be used as a tile, "homogenizing" a texture to eliminate splotchiness and gradations, and synthesizing an alpha channel from the color information. It operates on 1, 2, 3, or 4-channel SGI-format image files (gray-scale or RGB, with or without alpha).


MTEX

"mtex" is a specialized image processing tool for enhancing scanned images for use as texture maps. It is intended as the first step after scanning, and offers cropping, warping, contrast, color, sharpness, and homogeneity correction, edge blending, and chroma keying to synthesize alpha.

It handles SGI format image files with 1 to 4 components. It interprets the input file based on its number of components as:

                 # of components      Interpretation 
             -------------------------------------------
                       1             Gray-scale image
                       2             Gray-scale image with alpha 
                       3             RGB image
                       4             RGB image with alpha
  
....and can output images in any of these four formats.

mtex depends on overlay planes during cropping or warping, so INDIGOs will not be able to perform those functions. mtex performs texture mapping during the "Show texture In Use" function, so Personal Irises will not be able to display the current image in use.

mtex takes two optional command line arguments: an input filename and a "-nb" flag. If the input filename is not specified, mtex will prompt for one. "-nb" means "No Border", so that the output file will match the size of the input file if no resampling is performed. Otherwise, the outer edges of the scanned input image is treated as a (1/16 wide) border around all four sides, which improves the quality of resampling, sharpening, and edge blending, but which is not copied into the output file.

Main Menu

The main screen, with the main menu, is displayed initially. Selecting any main menu item puts you into a sub-screen with sub-menu for that specific function. You always return to the main screen before invoking a different function.

The main screen, as well as most sub-screens, shows a 512x512 version of the current image. Unless "-nb" was specified on the command line, the image displayed will be the original input image minus the 1/16 border around all sides. The displayed image is just point sampled (for display speed), so it may have aliasing artifacts and different resolution that the real image. If the input file (or resampled current image) is not square, its aspect ratio is preserved.

mtex uses only the left mouse button. Press buttons, control sliders, and select colors using it. The ESCAPE key is always available to abort mtex. Except when entering an input or output filename, the keyboard is otherwise disabled.

Crop, Warp

First select the size of the resulting image (More/Less X/Y, with or without a 2-pixel Mipmapping border). Then move the cropping rubberband around to position it, and hit "Performed Selected Cropping".

The "Square/Rectangular/Not Rotated/1:1 with Pixels" buttons constrain the rubberband quadrilateral. Not all combinations make sense or are possible. "Square" keeps the rubberband square, of course, while Rectangular keeps the angles 90 degrees. If "Not Rotated" is on with either "Square" or "Rectangular", then the rubberband sides will stay parallel to the image sides. If "1:1 with Pixels" is on, the rubberband will be forced to the cropped image size and shape, as previously set by "More/Less X/Y" and "Border".

To move the rubberband as a whole, press the left mouse button down while the cursor is near the middle of the rubberband shape. To move a single side (except if square), press just inside the middle of that side. To move a single vertex, press just inside the vertex. To rotate the entire quadrilateral (if enabled), press outside. "1:1 with Pixels" permits only movement, not resizing, reshaping, or rotation.

The source image is always sampled linearly. There is no perspective correction, since no third dimension is defined. Thus successive destination scanlines correspond to straight lines in the source image, with the ends of those lines falling at the proportional distance down the "sides" of the quadrilateral.

Brightness & Contrast

The luminance of each pixel can be adjusted using a transfer function which does not affect hue or saturation. If the transfer function requests a change which would put a color component above 255, the color is preserved and the luminance multiplier is reduced as necessary.

At the bottom right is the currently-defined transfer function. Initially it is a linear (1:1) ramp. To its left is a histogram of the current luminance values, with the same horizontal scale. The six sliders affect the position and shape of the transfer function curve. The "Brightness" (top) sliders move the shadow, mid-range, and highlight portions of the curve up or down (50 = middle). The "Contrast" sliders affect the slope of these portions of the curve. Of course, these influences overlap. The gamma LUT of the screen is updated continuously to match the transfer function, so the image on the screen is adjusted in real time. When the transfer function is what you want, press "Accept" and the pixels will be multiplied by the transfer function and the screen LUT returned to normal.

Adjust Color

The color of the pixels can be adjusted by the three sliders. The smaller 256x256 image is updated soon thereafter, so changes can be seen promptly. "Warmth" corresponds roughly to color temperature: up is more orange, and down is more blue. Tint corresponds roughly to a television "tint" knob: up is magenta, and down is green. Together, these two controls can move the image any direction in hue space. The luminance of every pixel is never changed. Saturation varies from a pure black-and-white image (0), through normal colors (100), to double the normal color saturation (200).

When the 256x256 image has the colors you want, press "Accept" to update the actual image. If the image was initially so far afield that the color controls do not have enough range, they can be applied repeatedly (using "Accept").

Blend Edges

This function modifies the edges of images to make the left side match the right side, or the top match the bottom. The matching is necessary if the texture map is used as a tile to hide the seam.

The "Top/Bottom Blend" and "Left/Right Blend" determine which edges are affected. Either or both can be set.

Both the "Simple" and the "Frequency Dependent" methods assumed that the two edges to be matched have borders which extend beyond the nominal image edge. (The image shown on the screen does not include this border). Thus when the images is folded to make the edges meet, the border of one side overlaps the pixels inside the other edge, and vice versa. This overlap provides an area where the pixels can be blended to allow a smooth transition.

The "Abruptness" slider affects the slope of the transition. The transition can be gradual, over many pixels, if the "Abruptness" slider is set low (100), or very fast (a pixel or two) if the "Abruptness" slide is high (999). The transition function is a symmetric Bezier spline.

The "Simple" method blends the overlapping pixels, fading in one edge and fading out the other. The transition does not occur at the same position for each row (or column) of pixels, however. For the left and right edges, for example, the overlapping pixels on each row are searched for the pair which are most equal in luminance. The result of that search is a jagged set of pixel positions down the image edge. This set of positions is smoothed, so there are no "spikes", and the smoothed locations used as the row-by-row centerpoints for the blending transition. Thus the blend snakes left and right as it goes down the sides of the image, avoiding differences. (The top/bottom blend is equivalent.) This method is good for blobby textures where the basic appearance on both sides is similar, but there are local features to be dodged.

The "Frequency Dependent" method is similar. The transition function is applied at the exact image edge without snaking back and forth, but it is applied to each frequency octave of the image separately. Thus low frequencies in the image (coarse features) are blended VERY gradually, whereas high frequencies (tiny detail) snap abruptly. Here again, the "Abruptness" slider scales the transition width. This approach avoids both double images (since the transition is always roughly the same size as the features) and noticeable edges. It is useful where there are substantial differences between the sides of the images.

The "2x2" and "6x6" buttons allow tiling of the image display to examine edge matching. Deselect both to see a single image.

Color to Alpha

An alpha channel can be synthesized (or replaced if one already exists) by an approach similar to Chroma-keying. Up to 12 foreground and 12 background colors can be selected by clicking on the image itself. These define two sets of colors which indicate objects which should have alpha 255 or 0 respectively. Pixel alpha is generated based on its color's closeness to the foreground and background colors.

Foreground color selection can be enabled by pressing "Foreground". Then click on colors of areas in the image you want to be opaque. The first 12 colors will appear under the "Foreground" button. Additional colors may be clicked on; they will replace members of the set if they are more distinctive than a previously-chosen color. Thus clicking all over the foreground colors builds up a group of 12 color extrema.

Background colors can be selected in the same way as foreground colors, using the "Background" button. For either group, if you select a color by mistake, click twice on the "Foreground" or "Background" button to erase that whole set, and start over.

Once the Foreground colors and Background colors are selected, press "Generate Alpha" to grade all the pixels in the image. Grading is done as follows. For a particular pixel, find the nearest foreground and nearest background color. "Nearest" means distance in a 3-D RGB color space, where green is stretched more than red, and red more than blue, to make distance more proportional to luminance. The distance metric is actually the sum of the cubes of the red, green, and blue distances, to punish colors which differ greatly in hue. Once the nearest foreground and background colors are ascertained, pass a line through their two points in this weird color space. Project the pixel's location on to that line. That position relative to the foreground and background points is the pixel's grade. Positions beyond the foreground or background point are clamped at 0 and 100% of the way from background to foreground.

This results in grades which have many pixels between 0 and 100%, meaning that many pixels would be partially transparent. To force grades near 0 down to 0, and near 100 up to 100, increase the "Margin" slider. At its maximum of 400, grades below 37.5 are rounded down to 0 and grades above 62.5 are rounded up to 100. Grades between 37.5 and 62.5 would become alpha values 1-254.

The "Margin" can be changed, and the results seen, without regrading all the pixels in the image. Just move the slider and press "Redraw".

The current alpha values can be seen in three ways. "Show Premultiplied" shows pure foreground colors unchanged, and darkens all other pixels by multiplying their alpha by them. Thus the pixels are effectively blended with a black background. "Show Fractional Alpha" gives a white-on-black display of only those pixels whose alpha values are 1-254. It should be a lacy, delicate web of pixels at object edges. If it is too coarse, increase the margin and redraw. "Show Pure Alpha" shows the alpha channel as a gray-scale image.

If the margin cannot adjust the alpha satisfactorily, choose new foreground and background colors and "Generate Alpha" again.

When the alpha channel appears correct, press "Accept". This will adjust the RGB values of those pixels which have alpha < 255, to eliminated the background color pollution of their RGB values. Alpha = 0 pixels are set to black. Alpha 1-254 pixels are set to the color of their neighbor which has the highest alpha, on the assumption that higher alpha means they are less polluted.

Adjust Sharpness

The sliders, one per octave of frequency, are a "Graphics Equalizer" which allow you to turn up or down specific frequency ranges. Thus turning up the small wavelengths will sharpen the image.

Scanned or resampled images often benefit from a little sharpening (~20%). Texture maps which will be tiled benefit from removal of all low-frequency information (the bottom few octaves), since it corresponds to blotchiness in the image which is visible in a repeating tile.

Select the frequency profile you want using the sliders, then press "Accept".

Homogenize

Texture maps showing true surface textures, such as sand, plaster, or cloth, which have no large-scale information and which will be tiled, should be uniform. Unfortunately, photography often makes for minor variations in exposure or focus or color.

"Homogenize" corrects gradual variations in brightness, contrast, color, or sharpness. Small-scale variations are not corrected. The image is sampled coarsely to create a 4x4 array of brightness, contrast, color, or sharpness values. A bi-cubic surface is passed through these 16 points to give a smooth error function. The selected image characteristic is corrected to make the error zero. Since the 4x4 sampling is very coarse, the variations must be gradual and large to be corrected.

Brightness means the pixel luminance (31% red, 61% green, and 8% blue). Hue and saturation are not changed by brightness adjustment.

Contrast is more difficult to measure. Each 1/16th of the image is examined with a luminance histogram. The 10% and 90% points in the histogram are used: the contrast is their difference divided by their average.

Color is just the average red, green, and blue intensity, treated separately. Thus it is equivalent to the Brightness function, applied to each color component by itself.

Sharpness is based on comparing each pixel to the average of its eight neighbors. A single pixel's "sharpness" is the ratio of the difference between it and its neighbors divided by the larger of the two. Thus it is, in a sense, a local contrast measurement.

Although all four characteristics can be measured and corrected simultaneously, there may be interactions, so it is better to apply them one at a time. Press "Homogenize" to perform the selected correction.

Show Texture In Use

A simple scene with a flat, tiled floor, a twisted torus, and a coarse fractal mountain is drawn, with all surfaces texture mapped. The mapping can match edges ("Continuous"), or be randomly selected within the texture image ("Random"). The texture sampling can be "Point", "Bilinear", or "Mipmapped". The mapping can also be turned 90 degrees ("Rotate Map") so that stripes can be radial or circumferential on the twisted torus, for example.

Save Image

The current image can be saved in an SGI format file. Either intensity (gray scale) or full RGB can be selected. If the image has an alpha channel, 2 or 4 components will be saved. If not, 1 or 3 components are used. If you did not do resampling and are concerned that the image saved is smaller (by 7/8ths) than the original image, it is because outer 1/16th was taken as a border for resampling, sharpening, and edge matching. If you are doing none of these, and want all of your image, use the "-nb" command line switch.

  
  
  
Bugs:

The "Different Image File" main menu button currently does nothing.

Cropped images which do not have a 1:1 aspect ratio will not get a correctly-shaped rubberband when "1:1 with Pixels" is requested.

Generating Alpha values if no Foreground or Background colors have been selected will cause a segment fault.

Color-to-alpha cannot draw or redraw alpha which it did not synthesize.

Show Texture In Use bombs if texture has alpha channel, and doesn't complain on a machine which does not perform texture mapping.


Files of interest include:




Select any combo of files you'd like to send yourself a compressed tar image of. Executables/scripts are indicated with a trailing `*' character. (Depending upon the browser, it may be necessary to hold down the Ctrl key to select/deselect disjoint items.) a compressed tar image of the above-selected items.
OR, ...
a compressed tar image of the entire mtex directory.

Copyright © 1995, Silicon Graphics, Inc.